package com.csu.cphone.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.csu.cphone.dao.PhoneBrandMapper;
import com.csu.cphone.dao.RecyclePhoneMapper;
import com.csu.cphone.dao.RecycleProblemMapper;
import com.csu.cphone.dao.RecycleProblemSelectMapper;
import com.csu.cphone.pojo.PhoneBrandPojo;
import com.csu.cphone.pojo.RecyclePhonePojo;
import com.csu.cphone.pojo.RecycleProblemPojo;
import com.csu.cphone.pojo.RecycleProblemSelectPojo;
import com.csu.cphone.service.RecycleService;
import com.csu.cphone.vo.RecyclePhoneProblemsVo;
import com.csu.cphone.vo.RecyclePhoneTreeVo;
import com.csu.cphone.vo.RecycleSelectAnswerVo;

/**
 * @项目名称：cphone
 * @类名称：RecycleServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月17日 下午9:16:59
 * @company:
 */
@Service
public class RecycleServiceImpl implements RecycleService
{

    @Autowired
    private RecycleProblemMapper recycleProblemMapper;

    @Autowired
    private RecycleProblemSelectMapper recycleProblemSelectMapper;

    @Autowired
    private RecyclePhoneMapper recyclePhoneMapper;

    @Autowired
    private PhoneBrandMapper phoneBrandMapper;

    /**
     * @see com.csu.cphone.service.RecycleService#getRecycleProblems(int)
     */
    @Override
    public List<RecycleProblemPojo> getRecycleProblems(int phoneId)
    {
        List<RecycleProblemPojo> problems = getProblemByPhoneId(phoneId);

        List<Integer> problemIds = new ArrayList<Integer>();
        for (RecycleProblemPojo problem : problems)
        {
            Integer problemId = problem.getId();
            problemIds.add(problemId);
        }

        List<RecycleProblemSelectPojo> selects = getProblemSelectByProblemIds(problemIds);

        Map<Integer, List<RecycleProblemSelectPojo>> selectMap = new HashMap<Integer, List<RecycleProblemSelectPojo>>();
        for (RecycleProblemSelectPojo select : selects)
        {
            Integer problemId = select.getProblemId();
            List<RecycleProblemSelectPojo> thisProblemSelects = selectMap.get(problemId);
            if (thisProblemSelects == null)
            {
                thisProblemSelects = new ArrayList<RecycleProblemSelectPojo>();
            }
            thisProblemSelects.add(select);
            selectMap.put(problemId, thisProblemSelects);
        }

        for (RecycleProblemPojo problem : problems)
        {
            Integer problemId = problem.getId();
            problem.setSelects(selectMap.get(problemId));
        }

        return problems;
    }

    /**
     * @see com.csu.cphone.service.RecycleService#offer(com.csu.cphone.vo.RecyclePhoneProblemsVo)
     */
    @Override
    public Double offer(RecyclePhoneProblemsVo phoneProblems)
    {
        int phoneId = phoneProblems.getPhoneId();
        RecyclePhonePojo phone = getPhonePojoByPhoneId(phoneId);
        double totalPrice = phone.getTotalPrice();

        List<Integer> selectIds = new ArrayList<Integer>();
        for (RecycleSelectAnswerVo selectAnswer : phoneProblems.getAnswers())
        {
            List<Integer> selecyIdList = selectAnswer.getSelectIds();
            if (!CollectionUtils.isEmpty(selecyIdList))
            {
                selectIds.addAll(selecyIdList);
            }
        }
        List<RecycleProblemSelectPojo> selectPojos = getSelectBySelectIds(selectIds);

        for (RecycleProblemSelectPojo selectPojo : selectPojos)
        {
            double discount = selectPojo.getCost();
            totalPrice = totalPrice - discount;
        }
        if (totalPrice < 5d)
        {
            totalPrice = 5d;
        }
        return totalPrice;
    }

    /**
     * @see com.csu.cphone.service.RecycleService#addRecycleProblem(com.csu.cphone.pojo.RecycleProblemPojo)
     */
    @Override
    public RecycleProblemPojo addRecycleProblem(RecycleProblemPojo phoneProblem)
    {
        List<RecycleProblemSelectPojo> selects = phoneProblem.getSelects();
        recycleProblemMapper.insert(phoneProblem);
        for (RecycleProblemSelectPojo select : selects)
        {
            select.setProblemId(phoneProblem.getId());
        }
        recycleProblemSelectMapper.batchInsert(selects);

        return phoneProblem;
    }

    /**
     * @see com.csu.cphone.service.RecycleService#getHotRecycleList()
     */
    @Override
    public List<RecyclePhonePojo> getHotRecycleList()
    {
        return recyclePhoneMapper.getHotRecycleList();
    }

    /**
     * @see com.csu.cphone.service.RecycleService#getRecyclePhoneList(int)
     */
    @Override
    public List<RecyclePhonePojo> getRecyclePhoneList(int brandId)
    {
        return recyclePhoneMapper.selectRecyclePhoneList(brandId);
    }

    /**
     * @see com.csu.cphone.service.RecycleService#getMaintainPhoneTreeList()
     */
    @Override
    public List<RecyclePhoneTreeVo> getMaintainPhoneTreeList()
    {
        List<RecyclePhoneTreeVo> recyclePhoneTreeList = new ArrayList<RecyclePhoneTreeVo>();

        List<PhoneBrandPojo> brands = phoneBrandMapper.selectBrandList();

        for (PhoneBrandPojo brand : brands)
        {
            Integer brandId = brand.getId();
            List<RecyclePhonePojo> recyclePhones = recyclePhoneMapper.selectRecyclePhoneList(brandId);

            RecyclePhoneTreeVo recyclePhoneTree = new RecyclePhoneTreeVo();
            recyclePhoneTree.setBrandId(brandId);
            recyclePhoneTree.setBrandName(brand.getBrandName());
            recyclePhoneTree.setRecyclePhones(recyclePhones);

            recyclePhoneTreeList.add(recyclePhoneTree);
        }

        return recyclePhoneTreeList;
    }

    /**
     * @see com.csu.cphone.service.RecycleService#addToHotRecycleList(com.csu.cphone.vo.HotPhone)
     */
    @Override
    public int addToHotRecycleList(int phoneId)
    {
        int result = recyclePhoneMapper.updateHot(phoneId);

        return result;
    }

    /**
     * @see com.csu.cphone.service.RecycleService#addPhone(int,
     *      java.lang.String, java.lang.String, int, double)
     */
    @Override
    public RecyclePhonePojo addPhone(int brandId, String name, String url, int isUseRecycle, double totalPrice,
            int isUseHotRecycle)
    {
        RecyclePhonePojo phone = new RecyclePhonePojo();
        phone.setBrandId(brandId);
        phone.setName(name);
        phone.setPicUrl(url);
        phone.setStatus(isUseRecycle);
        phone.setTotalPrice(totalPrice);
        phone.setIsHot(isUseHotRecycle);
        recyclePhoneMapper.insert(phone);
        return phone;
    }

    /**
     * @description
     * @author TangC
     * @date 2017年11月7日 上午11:13:55
     * @param problemIds
     * @return
     */

    private List<RecycleProblemSelectPojo> getProblemSelectByProblemIds(List<Integer> problemIds)
    {

        if (CollectionUtils.isEmpty(problemIds))
        {
            return new ArrayList<RecycleProblemSelectPojo>();
        }
        return recycleProblemSelectMapper.selectByProblemIds(problemIds);
    }

    /**
     * @description
     * @author TangC
     * @date 2017年11月7日 上午11:08:34
     * @param phoneId
     * @return
     */

    private RecyclePhonePojo getPhonePojoByPhoneId(int phoneId)
    {
        return recyclePhoneMapper.selectByPhoneId(phoneId);
    }

    /**
     * @description
     * @author TangC
     * @date 2017年11月7日 上午11:13:15
     * @param phoneId
     * @return
     */

    private List<RecycleProblemPojo> getProblemByPhoneId(int phoneId)
    {
        return recycleProblemMapper.selectByPhoneId(phoneId);
    }

    /**
     * @description
     * @author TangC
     * @date 2017年11月7日 上午11:07:00
     * @param answers
     * @return
     */

    private List<RecycleProblemSelectPojo> getSelectBySelectIds(List<Integer> answers)
    {
        if (CollectionUtils.isEmpty(answers))
        {
            return new ArrayList<RecycleProblemSelectPojo>();
        }
        return recycleProblemSelectMapper.selectBySelectIds(answers);
    }

    /**
     * @see com.csu.cphone.service.RecycleService#offerBySelects(java.util.List)
     */
    @Override
    public Double offerBySelects(List<RecycleProblemSelectPojo> selects, Integer phoneId)
    {
        RecyclePhonePojo phone = getPhonePojoByPhoneId(phoneId);
        double totalPrice = phone.getTotalPrice();
        Double dsicountPrice = 0d;
        for (RecycleProblemSelectPojo selectPojo : selects)
        {
            double discount = selectPojo.getCost();
            dsicountPrice = dsicountPrice + discount;
        }
        double result = totalPrice - dsicountPrice;
        if (result < 5d)
        {
            result = 5d;
        }
        return result;
    }

}
